package aash.utils.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Locale;
import org.apache.log4j.Logger;

/**
 * Class containing tools to connect by HTTP.
 * @author Aashintan
 */
public final class Connection {
    /**
     * Instance of the singleton Connection.
     */
    private static Connection instance = null;
    /**
     * Including each navigator available.
     */
    private HashMap < String, String > userAgent;
    /**
     * Including each language available.
     */
    private HashMap < Locale, String > acceptLanguage;
    /**
     * Logger.
     */
    private static Logger log = Logger.getLogger(Connection.class.getName());

    /**
     * Class constructor.
     */
    private Connection() {
        // Initialization of the navigators
        userAgent = new HashMap < String, String >();
        userAgent.put("Mozilla", "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.1.2)"
            + "Gecko/20090729 Firefox/3.5.2");
        // Initialization of the languages
        acceptLanguage = new HashMap < Locale, String >();
        acceptLanguage.put(Locale.FRANCE, "fr,fr-fr;q=0.8");
    }

    /**
     * Access singleton of Connection class.
     * @return the connection
     */
    public static Connection getInstance() {
        if (Connection.instance == null) {
            Connection.instance = new Connection();
        }
        return Connection.instance;
    }

    /**
     * Returning the source code of the page targeted by the URL.
     * @param stUrlAdress the page URL to target
     * @param stNavigator the Navigator to use : - Mozilla
     * @param loLanguage the Language to use
     * @return a String containing the page source code
     */
    public String getSourceCode(final String stUrlAdress, final String stNavigator, final Locale loLanguage) {
        String inputLine = null;
        URL url;
        try {
            url = new URL(stUrlAdress);
            URLConnection conn = url.openConnection();
            if ((stNavigator != null) && userAgent.containsKey(stNavigator)) {
                conn.setRequestProperty("User-Agent", userAgent.get(stNavigator));
            }
            if ((loLanguage != null) && acceptLanguage.containsKey(loLanguage)) {
                conn.setRequestProperty("Accept-Language", acceptLanguage.get(loLanguage));
            }
            conn.connect();
            StringWriter writer = new StringWriter();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = "";
            while (null != (line = in.readLine())) {
                writer.write(line);
            }
            inputLine = writer.toString();
            in.close();
        } catch (MalformedURLException e) {
            log.error("Error during connection to url \"" + stUrlAdress + "\" :" + e);
        } catch (IOException e) {
            log.error("Error during connection to url \"" + stUrlAdress + "\" :" + e);
        }
        return inputLine;
    }
}
